﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Insurer {

    public class ProgramModel  : Configuration.ConfigurationItemModel {
                
        #region Private Properties

        private Core.Insurer.Program program = null;

        #endregion


        #region Public Properties

        public override Int64 ConfigurationItemId { set { ProgramId = value; } }

        public override Core.CoreConfigurationObject ConfigurationItem { get { return program; } }


        public Int64 ProgramId {

            set {

                program = Application.Core.Insurer.Program (value);

                if (program == null) {

                    program = new Core.Insurer.Program (Application);

                }

            }

        }

        public Core.Insurer.Program Program { get { return program; } set { program = value; } }


        public System.Web.Mvc.SelectList InsurerSelectList {

            get {

                List<Core.Insurer.Insurer> insurers;

                insurers = (from currentInsurer in Application.Core.Insurer.Insurers (false)

                            // RESERVED IN CASE OF ADDING ENABLE/DISABLE CAPABILITY

                            select currentInsurer).ToList ();

                return new System.Web.Mvc.SelectList (insurers, "Id", "Name");

            }

        }

        public System.Web.Mvc.SelectList ProgramTypeSelectList {

            get {

                List<Core.Insurer.ProgramType> programTypes;

                programTypes = (from currentProgramType in Application.Core.Insurer.ProgramTypes (false)

                                  where ((currentProgramType.Enabled) || (currentProgramType.Id == Program.ProgramTypeId)) // ENABLED OR IN USE ONLY 

                                  select currentProgramType).ToList ();

                return new System.Web.Mvc.SelectList (programTypes, "Id", "Name");

            }

        }

        #endregion 


        #region Constructors

        public ProgramModel (Int64 forProgramId) {

            ProgramId = forProgramId;

            return;

        }

        public ProgramModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form); // BASE RELOADS THE ORIGINAL OBJECT TYPE


            // ALLOW UPDATING OF READ-ONLY PROPERTIES FOR NEW PROGRAMS

            if (Program.Id == 0) {

                Program.InsurerId = Data.Readers.FormReader.Int64FromForm (form, "Program.InsurerId");

                Program.ProgramTypeId = Data.Readers.FormReader.Int64FromForm (form, "Program.ProgramTypeId");


            }

            else {

                // RESET READ-ONLY PROPERTIES FOR EXISTING PROGRAMS 

                Program.Name = Application.Core.Insurer.Program (Program.Id, false).Name;

            }


            // EDITABLE PROPERTIES FOR NEW AND EXISTING 

            Program.ProgramTypeId = Data.Readers.FormReader.Int64FromForm (form, "Program.ProgramTypeId");

            Program.UniqueId = Data.Readers.FormReader.StringFromForm (form, "Program.UniqueId");

            Program.LegacyId = Data.Readers.FormReader.StringFromForm (form, "Program.LegacyId");


            return;

        }

        #endregion 

    }

}